<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><link rel="stylesheet" type="text/css" href="style.css" /><script type="text/javascript" src="highlight.js"></script></head><body><pre><span class="hs-comment">{- |
Module      :  Control.Monad.Reader
Copyright   :  (c) Andy Gill 2001,
               (c) Oregon Graduate Institute of Science and Technology 2001,
               (c) Jeff Newbern 2003-2007,
               (c) Andriy Palamarchuk 2007
License     :  BSD-style (see the file LICENSE)

Maintainer  :  libraries@haskell.org
Stability   :  experimental
Portability :  non-portable (multi-param classes, functional dependencies)

[Computation type:] Computations which read values from a shared environment.

[Binding strategy:] Monad values are functions from the environment to a value.
The bound function is applied to the bound value, and both have access
to the shared environment.

[Useful for:] Maintaining variable bindings, or other shared environment.

[Zero and plus:] None.

[Example type:] @'Reader' [(String,Value)] a@

The 'Reader' monad (also called the Environment monad).
Represents a computation, which can read values from
a shared environment, pass values from function to function,
and execute sub-computations in a modified environment.
Using 'Reader' monad for such computations is often clearer and easier
than using the 'Control.Monad.State.State' monad.

  Inspired by the paper
  /Functional Programming with Overloading and Higher-Order Polymorphism/,
    Mark P Jones (&lt;http://web.cecs.pdx.edu/~mpj/&gt;)
    Advanced School of Functional Programming, 1995.
-}</span><span>
</span><span id="line-37"></span><span>
</span><span id="line-38"></span><span class="hs-keyword">module</span><span> </span><span class="hs-identifier">Control.Monad.Reader</span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-39"></span><span>    </span><span class="annot"><span class="hs-comment">-- * MonadReader class</span></span><span>
</span><span id="line-40"></span><span>    </span><span class="annot"><a href="Control.Monad.Reader.Class.html#MonadReader"><span class="hs-identifier">MonadReader</span></a></span><span class="hs-special">(</span><span class="hs-glyph">..</span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-41"></span><span>    </span><span class="annot"><a href="Control.Monad.Reader.Class.html#asks"><span class="hs-identifier">asks</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-42"></span><span>    </span><span class="annot"><span class="hs-comment">-- * The Reader monad</span></span><span>
</span><span id="line-43"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#Reader"><span class="hs-identifier">Reader</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-44"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#runReader"><span class="hs-identifier">runReader</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-45"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#mapReader"><span class="hs-identifier">mapReader</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-46"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#withReader"><span class="hs-identifier">withReader</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-47"></span><span>    </span><span class="annot"><span class="hs-comment">-- * The ReaderT monad transformer</span></span><span>
</span><span id="line-48"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ReaderT"><span class="hs-identifier">ReaderT</span></a></span><span class="hs-special">(</span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ReaderT"><span class="hs-identifier">ReaderT</span></a></span><span class="hs-special">)</span><span class="hs-special">,</span><span>
</span><span id="line-49"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#runReaderT"><span class="hs-identifier">runReaderT</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-50"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#mapReaderT"><span class="hs-identifier">mapReaderT</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-51"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#withReaderT"><span class="hs-identifier">withReaderT</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-52"></span><span>    </span><span class="hs-keyword">module</span><span> </span><span class="annot"><a href="../../base/src/Control.Monad.html#"><span class="hs-identifier">Control.Monad</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-53"></span><span>    </span><span class="hs-keyword">module</span><span> </span><span class="annot"><a href="../../base/src/Control.Monad.Fix.html#"><span class="hs-identifier">Control.Monad.Fix</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-54"></span><span>    </span><span class="hs-keyword">module</span><span> </span><span class="annot"><a href="Control.Monad.Trans.html"><span class="hs-identifier">Control.Monad.Trans</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-55"></span><span>    </span><span class="annot"><span class="hs-comment">-- * Example 1: Simple Reader Usage</span></span><span>
</span><span id="line-56"></span><span>    </span><span class="annot"><span class="hs-comment">-- $simpleReaderExample</span></span><span>
</span><span id="line-57"></span><span>
</span><span id="line-58"></span><span>    </span><span class="annot"><span class="hs-comment">-- * Example 2: Modifying Reader Content With @local@</span></span><span>
</span><span id="line-59"></span><span>    </span><span class="annot"><span class="hs-comment">-- $localExample</span></span><span>
</span><span id="line-60"></span><span>
</span><span id="line-61"></span><span>    </span><span class="annot"><span class="hs-comment">-- * Example 3: @ReaderT@ Monad Transformer</span></span><span>
</span><span id="line-62"></span><span>    </span><span class="annot"><span class="hs-comment">-- $ReaderTExample</span></span><span>
</span><span id="line-63"></span><span>    </span><span class="hs-special">)</span><span> </span><span class="hs-keyword">where</span><span>
</span><span id="line-64"></span><span>
</span><span id="line-65"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Monad.Reader.Class.html"><span class="hs-identifier">Control.Monad.Reader.Class</span></a></span><span>
</span><span id="line-66"></span><span>
</span><span id="line-67"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#"><span class="hs-identifier">Control.Monad.Trans.Reader</span></a></span><span> </span><span class="hs-special">(</span><span>
</span><span id="line-68"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#Reader"><span class="hs-identifier">Reader</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#runReader"><span class="hs-identifier">runReader</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#mapReader"><span class="hs-identifier">mapReader</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#withReader"><span class="hs-identifier">withReader</span></a></span><span class="hs-special">,</span><span>
</span><span id="line-69"></span><span>    </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ReaderT"><span class="hs-identifier">ReaderT</span></a></span><span class="hs-special">(</span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#ReaderT"><span class="hs-identifier">ReaderT</span></a></span><span class="hs-special">)</span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#runReaderT"><span class="hs-identifier">runReaderT</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#mapReaderT"><span class="hs-identifier">mapReaderT</span></a></span><span class="hs-special">,</span><span> </span><span class="annot"><a href="../../transformers/src/Control.Monad.Trans.Reader.html#withReaderT"><span class="hs-identifier">withReaderT</span></a></span><span class="hs-special">)</span><span>
</span><span id="line-70"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="Control.Monad.Trans.html"><span class="hs-identifier">Control.Monad.Trans</span></a></span><span>
</span><span id="line-71"></span><span>
</span><span id="line-72"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Control.Monad.html#"><span class="hs-identifier">Control.Monad</span></a></span><span>
</span><span id="line-73"></span><span class="hs-keyword">import</span><span> </span><span class="annot"><a href="../../base/src/Control.Monad.Fix.html#"><span class="hs-identifier">Control.Monad.Fix</span></a></span><span>
</span><span id="line-74"></span><span>
</span><span id="line-75"></span><span class="hs-comment">{- $simpleReaderExample

In this example the @Reader@ monad provides access to variable bindings.
Bindings are a @Map@ of integer variables.
The variable @count@ contains number of variables in the bindings.
You can see how to run a Reader monad and retrieve data from it
with 'runReader', how to access the Reader data with 'ask' and 'asks'.

&gt; type Bindings = Map String Int;
&gt;
&gt;-- Returns True if the &quot;count&quot; variable contains correct bindings size.
&gt;isCountCorrect :: Bindings -&gt; Bool
&gt;isCountCorrect bindings = runReader calc_isCountCorrect bindings
&gt;
&gt;-- The Reader monad, which implements this complicated check.
&gt;calc_isCountCorrect :: Reader Bindings Bool
&gt;calc_isCountCorrect = do
&gt;    count &lt;- asks (lookupVar &quot;count&quot;)
&gt;    bindings &lt;- ask
&gt;    return (count == (Map.size bindings))
&gt;
&gt;-- The selector function to  use with 'asks'.
&gt;-- Returns value of the variable with specified name.
&gt;lookupVar :: String -&gt; Bindings -&gt; Int
&gt;lookupVar name bindings = maybe 0 id (Map.lookup name bindings)
&gt;
&gt;sampleBindings = Map.fromList [(&quot;count&quot;,3), (&quot;1&quot;,1), (&quot;b&quot;,2)]
&gt;
&gt;main = do
&gt;    putStr $ &quot;Count is correct for bindings &quot; ++ (show sampleBindings) ++ &quot;: &quot;;
&gt;    putStrLn $ show (isCountCorrect sampleBindings);
-}</span><span>
</span><span id="line-107"></span><span>
</span><span id="line-108"></span><span class="hs-comment">{- $localExample

Shows how to modify Reader content with 'local'.

&gt;calculateContentLen :: Reader String Int
&gt;calculateContentLen = do
&gt;    content &lt;- ask
&gt;    return (length content);
&gt;
&gt;-- Calls calculateContentLen after adding a prefix to the Reader content.
&gt;calculateModifiedContentLen :: Reader String Int
&gt;calculateModifiedContentLen = local (&quot;Prefix &quot; ++) calculateContentLen
&gt;
&gt;main = do
&gt;    let s = &quot;12345&quot;;
&gt;    let modifiedLen = runReader calculateModifiedContentLen s
&gt;    let len = runReader calculateContentLen s
&gt;    putStrLn $ &quot;Modified 's' length: &quot; ++ (show modifiedLen)
&gt;    putStrLn $ &quot;Original 's' length: &quot; ++ (show len)
-}</span><span>
</span><span id="line-128"></span><span>
</span><span id="line-129"></span><span class="hs-comment">{- $ReaderTExample

Now you are thinking: 'Wow, what a great monad! I wish I could use
Reader functionality in MyFavoriteComplexMonad!'. Don't worry.
This can be easily done with the 'ReaderT' monad transformer.
This example shows how to combine @ReaderT@ with the IO monad.

&gt;-- The Reader/IO combined monad, where Reader stores a string.
&gt;printReaderContent :: ReaderT String IO ()
&gt;printReaderContent = do
&gt;    content &lt;- ask
&gt;    liftIO $ putStrLn (&quot;The Reader Content: &quot; ++ content)
&gt;
&gt;main = do
&gt;    runReaderT printReaderContent &quot;Some Content&quot;
-}</span><span>
</span><span id="line-145"></span></pre></body></html>